import Vue from 'vue';
import axios from 'axios';
// import router from '@/router';
// import { notification, message } from 'ant-design-vue';
import { ACCESS_TOKEN } from '@/store/mutation-types';
// import store from '@/store';

const baseDomain = window._CONFIG['domianURL'];

const service = axios.create({
	baseURL: baseDomain,
	timeout: 10000
});
// 响应错误的处理
const err = (error) => {
	console.log(error)
	switch (error.status) {
		// if (error && error.response && error.response.status) {
		case 500:
			// notification.error({ message: '系统提示', description: '500错误，服务端无响应', duration: 4 });
			break;
	}
	return Promise.reject(error);
	// } else {
	// 	return Promise.reject(error);
	// }
};
// 请求拦截
service.interceptors.request.use(config => {
	const sToken = Vue.ls.get(ACCESS_TOKEN);
	if (sToken) {
		// config.headers['X-Access-Token'] = sToken; // 让每个请求携带自定义 token
		config.headers['token'] = sToken; // 让每个请求携带自定义 token

	}
	return config;
}, err => {
	return Promise.reject(err);
});
// 响应拦截
service.interceptors.response.use(response => {
	const responseStatus = response.data.status;
	if (responseStatus) {
		if (responseStatus === 200 ) {
			return response.data;
		} else if (responseStatus !== 200) {
			// 错误提示
			// message.error(response.data.message);
			return response.data;
		}
	} else {
		return response;
	}
}, err);

const installer = {
	vm: {},
	install(Vue) {
		// Vue.use(service);
		// Vue.axios = service;
		Vue.prototype.$axios = service;
	}
};
export {
	installer as VueAxios,
	service as axios
};
